<template>
  <div class="container">
    <div class="app-container">
      <div class="role-operate">
        <el-button
          size="mini"
          type="primary"
          @click="showDialog = true"
          @close="btnCancel"
          >添加角色</el-button
        >
      </div>
      <el-table :data="list">
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template v-slot="{ row }">
            <el-input
              v-if="row.isEdit"
              size="mini"
              v-model="row.editRow.name"
            />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <template v-slot="{ row }">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" />
            <span>{{
              row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <el-input
              v-if="row.isEdit"
              type="textarea"
              v-model="row.editRow.description"
            />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <el-button size="mini" type="primary" @click="btnEditOK(row)"
                >确定</el-button
              >
              <el-button size="mini" @click="row.isEdit = false"
                >取消</el-button
              >
            </template>
            <template v-else>
              <el-button size="mini" type="text" @click="btnPermission(row.id)"
                >分配权限</el-button
              >
              <el-button size="mini" type="text" @click="btnEditRow(row)"
                >编辑</el-button
              >
              <el-popconfirm
                title="这是一段内容确定删除吗?"
                @onConfirm="confirmDel(row.id)"
              >
                <el-button
                  slot="reference"
                  style="margin-left: 10px"
                  size="mini"
                  type="text"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <el-pagination
          :page-size="pageParams.pageSize"
          :current-page="pageParams.page"
          :total="pageParams.total"
          layout="prev,pager,next"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog width="500px" :visible.sync="showDialog" @close="btnCancel">
      <!-- 表单内容 -->
      <el-form
        label-width="120px"
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        title="新增角色"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" style="width: 300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用" prop="state">
          <el-switch
            size="mini"
            v-model="roleForm.state"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            type="textarea"
            :rows="3"
            style="width: 300px"
            size="mini"
            v-model="roleForm.description"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="btnOk"
                >确定</el-button
              >
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 放置权限弹层 -->
    <el-dialog :visible.sync="showPermissionDialog" title="分配权限">
      <!-- 放置权限数据 -->
      <el-tree
        node-key="id"
        :data="permissionData"
        :props="{ label: 'name' }"
        show-checkbox
        default-expand-all
        :default-checked-keys="permIds"
      />
    </el-dialog>

    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="mini" @click="btnPermissionOK"
          >确定</el-button
        >
        <el-button size="mini" @click="showPermissionDialog = false"
          >取消</el-button
        >
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {
  getRoleList,
  updateRole,
  deleteRole,
  addRole,
  getRoleDetail,
  assignPerm,
} from "@/api/role";
import { transListToTreeData } from "@/utils";
import { getPermissionList } from "@/api/permission";
export default {
  name: "Role",
  created() {
    this.getRoleList();
  },
  data() {
    return {
      currentRoleId: null,
      permIds: [],
      list: [],
      showDialog: false,
      pageParams: {
        page: 1,
        pageSize: 5,
        total: 0,
      },
      showPermissionDialog: false,
      permissionData: [],
      // isEdit: false,
      roleForm: {
        name: "",
        state: 0,
        description: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "角色名称不能为空",
            trigger: "blur",
          },
        ],
        description: [
          {
            required: true,
            message: "角色描述不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async btnPermissionOK() {
      await assignPerm({
        id: this.currentRoleId,
        permIds: this.$refs.permTree.getCheckedKeys(),
      });
      this.$message.success("角色分配权限成功");
      this.showPermissionDialog = false;
    },
    async btnPermission(id) {
      this.currentRoleId = id;
      const { permIds } = await getRoleDetail(id);
      this.permIds = permIds;
      this.permissionData = transListToTreeData(await getPermissionList(), 0);
      this.showPermissionDialog = true;
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageParams);
      console.log(rows);
      console.log(total);
      this.list = rows;
      this.pageParams.total = total;
      this.list.forEach((item) => {
        // item.isEdit = false;
        //动态属性 动态绑定
        this.$set(item, "isEdit", false);
        this.$set(item, "editRow", {
          name: item.name,
          state: item.state,
          description: item.description,
        });
      });
    },
    changePage(newPage) {
      this.pageParams.page = newPage;
      this.getRoleList();
    },
    btnOk() {
      this.$refs.roleForm.validate(async (isOk) => {
        if (isOk) {
          await addRole(this.roleForm);
          this.$message.success("新增角色成功");
          this.getRoleList();
          this.btnCancel();
          console.log(123);
        }
      });
    },
    btnCancel() {
      this.$refs.roleForm.resetFields();
      this.showDialog = false;
    },
    btnEditRow(row) {
      row.isEdit = true;
      row.editRow.name = row.name;
      row.editRow.state = row.state;
      row.editRow.description = row.editRow.description;
    },
    async btnEditOK(row) {
      console.log(row);
      if (row.editRow.name && row.editRow.description) {
        await updateRole({ ...row.editRow, id: row.id });
        this.$message.success("编辑角色成功");
        Object.assign(row, {
          ...row.editRow,
          isEdit: false,
        });
      } else {
        this.$message.warning("名称和描述不能为空");
      }
    },
    async confirmDel(id) {
      await deleteRole(id);
      this.$message("删除角色成功");
      if (this.length === 1) this.pageParams.page--;
      this.getRoleList();
    },
  },
};
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
